package com.example.demo.service;

import com.example.demo.enums.BookStatus;
import com.example.demo.mapper.BookMapper;
import com.example.demo.model.Book;
import com.example.demo.model.PageRequest;
import com.example.demo.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;
    public Integer insertBook(Book book){
        return bookMapper.insertBook(book);
    }

    public PageResult<Book> selectBookByPage(PageRequest pageRequest) {
        List<Book> books = bookMapper.selectBookByPage(pageRequest);
        for(Book book : books){
            book.setStatusCN(BookStatus.getBookStatusByCode(book.getStatus()).getName());
        }
        Integer count = bookMapper.selectBookTotalCount();
        PageResult<Book> pageResult = new PageResult<>();
        pageResult.setRecords(books);
        pageResult.setCount(count);
        pageResult.setPageRequest(pageRequest);
        return pageResult;
    }

    public Book selectBookById(Integer id) {
        Book book = bookMapper.selectBookById(id);
        book.setStatusCN(BookStatus.getBookStatusByCode(book.getStatus()).getName());
        return book;
    }

    public Integer updateBookById(Book book) {
        return bookMapper.updateBookById(book);
    }

    public Integer deleteBookById(Integer id) {
        return bookMapper.deleteBookById(id);
    }

    public Integer batchDeleteByIds(List<Integer> ids) {
        return bookMapper.batchDeleteByIds(ids);
    }
}
